﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using COFE.Core;
using COFE.Core.Utils;
using COFE.Core.Entry;

namespace COFE.Core
{
    public static class IPropertyProviderExtension
    {

        ///// <summary>
        ///// Return a list of supported property.
        ///// </summary>
        ///// <returns></returns>
        //public static object[] GetSupportedProperty(this IPropertyProvider primPropProvider)
        //{
        //    return new List<object>(from prop in primPropProvider.GetSupportedProperty()
        //                            where primPropProvider.IsPropertySupported(prop)
        //                            select prop).ToArray();
        //}


        /// <summary>
        /// Return whether a property is supported.
        /// </summary>
        /// <typeparam name="A"></typeparam>
        /// <param name="primPropProvider"></param>
        /// <param name="property"></param>
        /// <returns></returns>
        public static bool IsPropertyReadOnly(this IPropertyProvider propProvider, object property)
        {
            var supportOptions = propProvider.GetPropertySupportOptions(property);
            return supportOptions.HasFlag(PropertySupportOptions.CanRead) && !supportOptions.HasFlag(PropertySupportOptions.CanWrite);
        }

        /// <summary>
        /// Return whether a property is supported.
        /// </summary>
        /// <typeparam name="A"></typeparam>        
        /// <param name="property"></param>
        /// <returns></returns>
        public static bool IsPropertySupported(this IPropertyProvider propProvider, object property)
        {
            var supportOptions = propProvider.GetPropertySupportOptions(property);
            return supportOptions.HasFlag(PropertySupportOptions.CanRead);
        }


        /// <summary>
        /// Return a comma delimited string that documented the supported property number (lookup it from PropertyDictionary).
        /// </summary>
        /// <param name="primPropProvider"></param>
        /// <returns></returns>
        public static string GetSupportedPropertyString(this IPropertyProvider propProvider)
        {
            return String.Join(",",(from prop in propProvider.GetSupportedProperty()                                    
                                    select (int)prop));
        }

        public static string GetWritablePropertyString(this IPropertyProvider propProvider)
        {
            return String.Join(",", (from prop in propProvider.GetSupportedProperty()
                                     where propProvider.GetPropertySupportOptions(prop).HasFlag(PropertySupportOptions.CanWrite)
                                     select (int)prop));
        }

        public static string GetParseName(this IPropertyProvider propProvider)
        {
            if (propProvider.IsPropertySupported(FileSystemInfoProperties.FullParseName))
                return propProvider.GetProperty(FileSystemInfoProperties.FullParseName).ValueAsString;
            throw new NotImplementedException("This item does not implement ParseName or FullParseName");
        }

        public static Guid GetId(this IPropertyProvider propProvider)
        {
            return StringUtils<IFileSystemInfo>.GetDeterministicGuid(propProvider.GetParseName());
        }
    }
}
